//
//  MainTabBar.swift
//  3月5的项目
//
//  Created by 哦哦 on 16/3/6.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {
    
    //    init () {
    //
    //        super.init(frame: CGRectZero)
    //
    //
    //    }
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        addSubview(plusBtn)
        
    }
    
    
    
    required init?(coder aDecoder: NSCoder) {
        //        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
    }
    //自定义tabbar 需要将之前的每一个 tabbarbutton 的大小减小 并且将中间位置空出来
    //addSubView
    //子视图的frame 发生了变化
    //一般不建议在layoutsubViews中添加子视图
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //调式打印
        //UITabBarButton 是私有类
        let w = UIScreen.mainScreen().bounds.width/5
        let h = self.bounds.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        //定义一个递增的索引
        var index : CGFloat = 0
        for subView in subviews {
            
            if subView .isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                subView.frame = CGRectOffset(rect, index * w, 0)
                
                index += (index == 1 ? 2 : 1)
            }
            
        }
        
        plusBtn.frame = CGRectOffset(rect, 2 * w, 0)
        
    }
    
    //MARK: 懒加载子视图
    
    lazy var plusBtn:UIButton = UIFactory.buttonWithImage("tabbar_compose_icon_add", backImageName: "tabbar_compose_button")
//        //UIButton() 就是自定义样式的按钮
//        let btn = UIButton()
//        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState:.Normal )
//        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
//        
//        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
//        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
//        
//        
//        return btn
//    }()
//    
}
